function queryPara(para) {
  var url = window.location.search.substr(1);
  var reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)", "i");
  var r = url.match(reg);
  return r == null ? null : unescape(r[2]);
}
function ensureInFrame(fallbackUrl) {
  if (window.top == window) {
    window.location = fallbackUrl;
  }
}
var path = window.location.pathname;
var disableCheck = window.disableCheck || false;
var dbguid = "";
if (path != "/index.html" && disableCheck == false) {
  var mains = ["/main.html", "/mysql_main.html"];
  if (mains.includes(path)) {
    //这个页面就是main.html
    dbguid = queryPara("guid");
    if (!dbguid) {
      //未选择指定的数据库
      alert("未选中任何数据库!");
      window.location.href = "index.html";
    }
  } else {
    //这个页面不是main.html 找父页面
    (function getDbHtml(win) {
      if (mains.includes(win.location.pathname)) {
        dbguid = win.queryPara("guid");
        if (!dbguid) {
          //未选择指定的数据库
          win.alert("未选中任何数据库!");
          win.location.href = "index.html";
        }
      } else {
        if (win.parent != win) {
          getDbHtml(win.parent);
        } else {
          //未选择指定的数据库
          win.alert("未选中任何数据库!");
          win.location.href = "index.html";
        }
      }
    })(window.parent);
  }

  $.ajaxSetup({
    headers: {
      "db-guid": dbguid
    }
  });
}

function showLoading(ele) {
  if (typeof ele == "string") {
    ele = $(ele);
  } else {
    ele = $(ele);
  }
  if (ele.length == 0) {
    ele = $(document.body);
  }
  var pos = $(ele).offset();
  var wid = $(ele).outerWidth();
  var hei = $(ele).outerHeight();
  var str = `<div class="loading-area"></div>`;
  var loadDom = $(str)
    .appendTo(document.body)
    .css("top", pos.top + "px")
    .css("width", wid + "px")
    .css("height", hei + "px")
    .css("left", pos.left)
    .show();
  return loadDom;
}
function hideLoading() {
  $(".loading-area").remove();
}

function getCurrentDb() {
  var conns = null;
  $.ajax({
    type: "get",
    url: "/config/connections.json?_t=" + new Date().getTime(),
    async: false,
    success: function (res) {
      conns = res;
    }
  });
  if (conns) {
    for (var i = 0; i < conns.length; i++) {
      if (conns[i].Guid == dbguid) {
        var res = conns[i];
        res.db = res.Db;
        res.guid = res.Guid;
        res.type = res.Type;
        res.name = res.Name;
        res.addr = res.Addr;
        res.db = res.Db;
        res.user = res.User;
        res.pwd = res.Pwd;
        return res;
      }
    }
  }
  return {};
}

function copy(ele) {
  var inp = $(ele)[0];
  // is element selectable?
  if (inp && inp.select) {
    // select text
    inp.select();
    try {
      // copy text
      document.execCommand("copy");
      inp.blur();
    } catch (err) {
      alert("please press Ctrl/Cmd+C to copy");
    }
  }
}

var HtmlUtil = {
  /*1.用浏览器内部转换器实现html编码（转义）*/
  htmlEncode: function (html) {
    //1.首先动态创建一个容器标签元素，如DIV
    var temp = document.createElement("div");
    //2.然后将要转换的字符串设置为这个元素的innerText或者textContent
    temp.textContent != undefined
      ? (temp.textContent = html)
      : (temp.innerText = html);
    //3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
    var output = temp.innerHTML;
    temp = null;
    return output;
  },
  /*2.用浏览器内部转换器实现html解码（反转义）*/
  htmlDecode: function (text) {
    //1.首先动态创建一个容器标签元素，如DIV
    var temp = document.createElement("div");
    //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
    temp.innerHTML = text;
    //3.最后返回这个元素的innerText或者textContent，即得到经过HTML解码的字符串了。
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
  },
  /*3.用正则表达式实现html编码（转义）*/
  htmlEncodeByRegExp: function (str) {
    var temp = "";
    if (str.length == 0) return "";
    temp = str.replace(/&/g, "&");
    temp = temp.replace(/</g, "<");
    temp = temp.replace(/>/g, ">");
    temp = temp.replace(/\s/g, " ");
    temp = temp.replace(/\'/g, "'");
    temp = temp.replace(/\"/g, "&quot");
    return temp;
  },
  /*4.用正则表达式实现html解码（反转义）*/
  htmlDecodeByRegExp: function (str) {
    var temp = "";
    if (str.length == 0) return "";
    temp = str.replace(/&/g, "&");
    temp = temp.replace(/</g, "<");
    temp = temp.replace(/>/g, ">");
    temp = temp.replace(/ /g, " ");
    temp = temp.replace(/'/g, "'");
    temp = temp.replace(/"/g, '"');
    return temp;
  },
  /*5.用正则表达式实现html编码（转义）（另一种写法）*/
  html2Escape: function (sHtml) {
    return sHtml.replace(/[<>&"]/g, function (c) {
      return { "<": "<", ">": ">", "&": "&", '"': '"' }[c];
    });
  },
  /*6.用正则表达式实现html解码（反转义）（另一种写法）*/
  escape2Html: function (str) {
    var arrEntities = { lt: "<", gt: ">", nbsp: " ", amp: "&", quot: '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function (all, t) {
      return arrEntities[t];
    });
  }
};
